#!/usr/bin/env python

import sys
import os
from optparse import OptionParser
import paramiko

def getopt():
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-f", "--localfile", dest="localfile",
        help="specify the file on local host in full path")
    parser.add_option("-s", "--remotehost", dest="remotehost",
        help="specify the remote host")
    parser.add_option("-d", "--remotefile", dest="remotefile",
        help="specify the file saved on remort host in full path")
    parser.add_option("-u", "--user", dest="user",
        help="specify the login user on remote server, default is current user on localhost")
    parser.add_option("-p", "--port", type="int",dest="port", default=22,
        help="specify sshd listen port on remote server, default is 22")

    (options, args) = parser.parse_args()
        
    if options.localfile == None or options.remotehost == None or \
        options.remotefile == None or options.user == None:
        parser.print_help()
        sys.exit(1)

    return options

def main():
    opts = getopt()

    privatekeyfile=os.path.expanduser('~/.ssh/id_rsa')
    mykey=paramiko.RSAKey.from_private_key_file(privatekeyfile)
    username=opts.user
    transport=paramiko.Transport((opts.remotehost, opts.port))
    transport.connect(username=opts.user, pkey=mykey)

    sftp=paramiko.SFTPClient.from_transport(transport)
    sftp.put(opts.localfile, opts.remotefile)

    sftp.close()
    transport.close()


if __name__ == '__main__':
    main()

